package com.edu.app.controller.customize;

import com.edu.app.bean.core.customize.SysCustomerExtensionYlyn;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.customize.SysCustomerExtensionYlynService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/ylyn")
@RestController
public class ylynController {
    @Autowired
    private SysCustomerExtensionYlynService sysCustomerExtensionYlynService;

    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getCustomerDetails(){
        try{
            List<SysCustomerExtensionYlyn> sysCustomerExtensionYlynList = sysCustomerExtensionYlynService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_YLYN_CUSTOMER_DETAILS_SUCCEED, "ylyn_details_list", sysCustomerExtensionYlynList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @GetMapping(value = "/getCustomerDetailsById")
    @RequiresAuthentication
    public ModelMap getCustomerDetailsById(@RequestParam Integer customer_id){
        try{
            SysCustomerExtensionYlyn sysCustomerExtensionYlyn = sysCustomerExtensionYlynService.getCustomerDetailsByCustomerId(customer_id);
            if(sysCustomerExtensionYlyn != null){
                return MsgUtil.retMsg(SysMessage.GET_YLYN_CUSTOMER_DETAILS_BY_ID_SUCCEED, "ylyn_details", sysCustomerExtensionYlyn);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_DETAILS_NOT_EXISTS);
            }
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addCustomerDetails(@RequestHeader("Authorization") String token, @RequestBody SysCustomerExtensionYlyn sysCustomerExtensionYlyn){
        try{
            String username = JWTUtil.getUsername(token);
            sysCustomerExtensionYlyn.initializeSystemColumns(username);
            sysCustomerExtensionYlynService.add(sysCustomerExtensionYlyn);
            return MsgUtil.retMsg(SysMessage.ADD_YLYN_CUSTOMER_DETAILS_SUCCEED, null, null);
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_DETAILS_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap updateCustomerDetails(@RequestHeader("Authorization") String token, @RequestBody SysCustomerExtensionYlyn sysCustomerExtensionYlyn){
        try{
            String username = JWTUtil.getUsername(token);
            sysCustomerExtensionYlyn.initializeSystemColumns(username);
            sysCustomerExtensionYlynService.update(sysCustomerExtensionYlyn);
            return MsgUtil.retMsg(SysMessage.UPDATE_YLYN_CUSTOMER_DETAILS_SUCCEED, null, null);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteCustomerDetails(@RequestParam Integer id){
        try{
            sysCustomerExtensionYlynService.delete(id);
            return MsgUtil.retMsg(SysMessage.DELETE_YLYN_CUSTOMER_DETAILS_SUCCEED, null, null);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }
}
